package com.mdc.admin.controller;

import com.mdc.admin.component.MdcPrinter;
import com.mdc.log.annotation.SysLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * mdc测试
 *
 * @author pcheng reviewcode@163.com
 * @date 22:39 2021/7/31
 */
@Slf4j
@RestController
@RequestMapping("/mdc")
@RequiredArgsConstructor
public class MdcController {
    private final MdcPrinter mdcPrinter;

    @GetMapping
    @SysLog(value = "/mdc测试")
    public String printMdc(String mdcId) {
        Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
        log.info(copyOfContextMap.toString());
        mdcPrinter.printLog();
        return copyOfContextMap.toString();
    }

    @GetMapping("/noArgs")
    @SysLog(value = "mdc测试")
    public String printMdcNoArgs() {
        Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
        int a = 1 / 0;
        log.info(copyOfContextMap.toString());
        mdcPrinter.printLog();
        return copyOfContextMap.toString();
    }

    @PostMapping("/addMdc")
    @SysLog(value = "mdc测试")
    public List<String> printMdcPost(Map<String, String> paramMap) {
        log.info("{}", paramMap.toString());
        return Stream.of("1", "2").collect(Collectors.toList());
    }
}
